;;(require-package 'org-fstree)
;;(require-package 'org-magit)

;; 设置org不同等级的字体
;;(set-face-attribute 'org-level-1 nil :height 1.2 :bold t)
;;(set-face-attribute 'org-level-1 nil :height 1.1 :bold t)
;; 查看orgmode导出类型：http://orgmode.org/worg/exporters/ox-overview.html
;; 使能orgmode导出为markdown的选项(默认没有开启该功能)
(eval-after-load "org" '(require 'ox-md nil t))
;; 添加org导出epub电子书选项。要求orgmode9和zip支持
(require-package 'ox-epub)
(eval-after-load "org" '(require 'ox-epub nil t))

;; toc-org 用于产生目录
;; 在需要产生目录的headline(一般为一级标题)处，先添加:TOC:属性，在执行"toc-org-insert-toc"函数创建目录
(require-package 'toc-org)
(add-hook 'org-mode-hook 'toc-org-enable)
;; 在org-mode中开启图片查看模式
(add-hook 'org-mode-hook 'iimage-mode)

;; orgmode的一些基本配置
(setq org-log-done t ;;TODO状态发生改变时，添加时间戳等信息
;;      org-completion-use-ido t ;;使用ido-mode补全模式，ido-mode必须开启
;;      org-edit-timestamp-down-means-later t
;;      org-agenda-start-on-weekday nil
;;      org-agenda-span 14
      org-agenda-include-diary t
;;      org-agenda-window-setup 'current-window
;;      org-fast-tag-selection-single-key 'expert
;;      org-export-kill-product-buffer-when-displayed t
;;      org-tags-column 80
      org-startup-indented t)

; Refile targets include this file and any file contributing to the agenda - up to 5 levels deep
(setq org-refile-targets (quote ((nil :maxlevel . 5) (org-agenda-files :maxlevel . 5))))
; Targets start with the file name - allows creating level 1 tasks
(setq org-refile-use-outline-path (quote file))
; Targets complete in steps so we start with filename, TAB shows the next level of targets etc
(setq org-outline-path-complete-in-steps t)

;;--------------------------- org-clock简单配置 ------------------------------------------
;; 关于org-clock相关配置，org-clock-in开始为工作计时，org-clock-out工作停止计时。
;; org-clock大部分功能绑定在"C-c C-x"为前缀的功能键上。
(org-clock-persistence-insinuate)
(setq org-clock-persist t)
(setq org-clock-in-resume t)

;; todo事项开始计时自动进入"STARTED"状态
(setq org-clock-in-switch-to-state "STARTED")

;; 将计时记录数据保存到"LOGBOOK"抽屉(drawer)中
(setq org-clock-into-drawer t)
;; 记录时间格式为0:00(h:mm)
(setq org-clock-out-remove-zero-time-clocks t)

;; Show the clocked-in task - if any - in the header line
;;(defun lugg/show-org-clock-in-header-line ()
;;  (setq-default header-line-format '((" " org-mode-line-string " "))))

;;(defun lugg/hide-org-clock-from-header-line ()
;;  (setq-default header-line-format nil))

;;(add-hook 'org-clock-in-hook 'lugg/show-org-clock-in-header-line)
;;(add-hook 'org-clock-out-hook 'lugg/hide-org-clock-from-header-line)
;;(add-hook 'org-clock-cancel-hook 'lugg/hide-org-clock-from-header-line)

;;(eval-after-load 'org-clock
;;  '(progn
;;     (define-key org-clock-mode-line-map [header-line mouse-2] 'org-clock-goto)
;;     (define-key org-clock-mode-line-map [header-line mouse-1] 'org-clock-menu)))
;;--------------------------- org-clock简单配置 ------------------------------------------

;-----------------------------  orgmode插入编程代码的配置开始 -----------------------------
;; 激活一些常用的编程语言支持
;; 包括plantuml绘图，dot绘图，执行python代码等
(after-load 'org
	(if (< emacs-major-version 26)
			(org-babel-do-load-languages
			 'org-babel-load-languages
			 '(;(R . t)
																				;(gnuplot . t)
																				;(haskell . nil)
																				;(latex . t)
																				;(octave . t)
																				;(ruby . t)
				 (emacs-lisp . t) ;;elisp语言支持
				 (dot . t) ;;支持dot绘图语言绘图
				 (plantuml . t) ;;支持uml绘图
																				;(ditaa . t) ;;ditaa绘图支持
				 (python . t) ;;支持python语言
				 (sh . t)))
		(org-babel-do-load-languages
		 'org-babel-load-languages
		 '(;(R . t)
																				;(gnuplot . t)
																				;(haskell . nil)
																				;(latex . t)
																				;(octave . t)
																				;(ruby . t)
			 (emacs-lisp . t) ;;elisp语言支持
			 (dot . t) ;;支持dot绘图语言绘图
			 (plantuml . t) ;;支持uml绘图
																				;(ditaa . t) ;;ditaa绘图支持
			 (python . t) ;;支持python语言
			 (shell . t)))));;支持
(setq org-plantuml-jar-path
      (expand-file-name "/usr/share/java/plantuml.jar"));设置plantuml.jar的实际地址
;; 在begin_src中实现代码高亮
(setq org-src-fontify-natively t)
;-----------------------------  orgmode插入编程代码的配置结束 -----------------------------

;-----------------------------  org发布系统配置开始 -----------------------------
;;导出后C语言等语法高亮
(require-package 'htmlize)
;; 设置本地html文件保存地址
(setq my-org-publish-directory "~/html/")
;; 上传发布的html文档
(defun put-idev-files ()
  "上传智能设备文档到服务器"
  (interactive)
  (copy-directory (concat my-org-publish-directory "idev") "/ftp:www@ihomeonline.net:/html/documents/"))

;; 保证每次发布时都能重新导出每一个文件
(setq org-publish-use-timestamps-flag nil)
;; 注意网上很多资料publishing-function org-publish-org-to-html
;; 现在的版本更为 org-html-publish-to-html
;; 发布快捷键为
(setq org-publish-project-alist
      '(("idev-docs"
         :base-directory "~/design/idev/doc"
         :base-extension "org"
         :publishing-directory (concat my-org-publish-directory "idev")
         :publishing-function org-html-publish-to-html
         :exclude "设计说明书.org"
         :headline-levels 3
         :section-numbers nil
         :with-toc t
         :html-head "<link rel=\"stylesheet\" type=\"text/css\" href=\"../css/readtheorg.css\"/>"
         :html-preamble t)
        ("idev-ldpdf"
         :base-directory "~/design/idev/doc/pdf"
         :base-extension "pdf"
         :publishing-directory (concat my-org-publish-directory "idev/pdf")
         :publishing-function org-publish-attachment)
        ("idev-images"
         :base-directory "~/design/idev/doc/img"
         :base-extension "jpg\\|png\\|gif"
         :publishing-directory (concat my-org-publish-directory "idev/img")
         :publishing-function org-publish-attachment)
        ("iserverd-docs"
         :base-directory "~/design/IServerd/docs"
         :base-extension "org"
         :exclude "improving.org\\|specification.org"
         :publishing-directory (concat my-org-publish-directory "iserverd")
         :publishing-function org-html-publish-to-html
         :headline-levels 3
         :section-numbers nil
         :with-toc t
         :html-head "<link rel=\"stylesheet\" type=\"text/css\" href=\"../css/readtheorg.css\"/>"
         :html-preamble t)
        ("iserverd-images"
         :base-directory "~/design/IServerd/docs/img"
         :base-extension "jpg\\|png\\|gif"
         :publishing-directory (concat my-org-publish-directory "iserverd/img")
         :publishing-function org-publish-attachment)
        ("style"
         :base-directory "~/.emacs.d/css"
         :base-extension "css"
         :publishing-directory (concat my-org-publish-directory "css")
         :publishing-function org-publish-attachment)
        ("iserverd"
		 :components("iserverd-docs" "style" "iserverd-images"))
        ("idev"
		 :components("idev-docs" "style" "idev-ldpdf" "idev-images"))
        ("all"
		 :components("idev-docs" "style" "idev-ldpdf" "idev-images" "iserverd-docs" "iserverd-images"))))
;----------------------  org发布系统配置结束 -----------------------------

;---------------------- GTD配置开始 --------------------------------------------
; 当任务完成时，自动添加时间
(setq org-log-done 'time)
;; 配置org-capture<原org-remember> orgmode8以后使用org-capture
(require 'org-capture)
;TODO     最基本的任务状态，现在想做，但还没有计划做的时间
;NEXT     任务或想法提出，等等下一步行动，还没有开始计划
;WAITTING 等待其他人完成
;SOMEDAY  已经提上计划日程的莫一天
;DONE     已经完成，需要记录完成实际和备注说明，转移到Finished.org的相应节点
;PROJECT  开始执行的工程项目
;END	  任务已经结束
;ABORT    取消的任务，需要说明取消的理由，转移到Trash.org的相应节点
(setq org-todo-keywords
      ; TODO--任务安排; STARTED--任务开始执行; DONE--任务完成
      '((sequence "TODO(t)" "STARTED(s!)" "|" "DONE(d@/!)") ; 普通任务
        ; NEXT--工程项目想法提出； WAITTING--等待条件成熟； SOMEDAY--安排上日程； PROJECT--工程项目执行中; END--工程完成; ABORT--丢弃项目
        (sequence "NEXT(n)" "WAITTING(w)" "SOMEDAY(S)" "PROJECT(P!)" "|" "END(e@/!)" "ABORT(a@/!)") ;项目任务
        ; CREATE--任务开始，考虑构架等; CODEING--编写代码; TEST--编写完成，测试中； SAVE--测试完成，已存档
        (sequence "CREATE(c)" "CODEING(C!)" "|" "TEST(T@/!)" "SAVE(v@/!)") ;编程任务
        (sequence "创建(o!)" "执行中(r!)" "|" "完成(E@/!)")))
(setq org-tag-alist-for-agenda
      '(("@Office" . ?o) ("@Home" . ?h) ("@Design" . ?d)))
;New      收集未整理的信息
;Task     待办事项，所有未完成的事项
;Calendar 日程安排，具有明确时间的待办事项，可以是周期性任务
;Idea     想法、愿望
;Finished 所有完成的事项
;Project  项目任务
(setq org-capture-templates
      '(("n" "New" entry (file "~/gtd/inbox.org") "* TODO %?\n %i\n %T\n%a")
        ("t" "Task" entry (file+headline "~/gtd/task.org" "Task") "* TODO %?\n %i\n %T\n%a")
        ("c" "Calendar" entry (file+headline "~/gtd/task.org" "Calendar") "* TODO %?\n %i\n %T\n%a")
        ("d" "Idea" entry (file+headline "~/gtd/task.org" "Idea") "* TODO %?\n %i\n %T\n%a")
        ("f" "Finished" entry (file "~/gtd/finished.org") "* TODO %?\n %i\n %T\n")
        ("i" "Prj-idev" entry (file+headline "~/gtd/project.org" "Idev") "* TODO %?\n %i\n %T\n")
        ("h" "Prj-iserver" entry (file+headline "~/gtd/project.org" "IServer") "* TODO %?\n %i\n %T\n")
        ("s" "Prj-insys" entry (file+headline "~/gtd/project.org" "Insys") "* TODO %?\n %i\n %T\n")
        ("p" "Project" entry (file "~/gtd/project.org") "* TODO %?\n %i\n %T\n")))
;(setq org-capture-templates
;      '(("n" "New" entry (file "/lgg@ihomeonline.net:/home/lgg/gtd/inbox.org") "* TODO %?\n %i\n %T\n%a")
;        ("t" "Task" entry (file+headline "/lgg@ihomeonline.net:/home/lgg/gtd/task.org" "Task") "* TODO %?\n %i\n %T\n%a")
;        ("c" "Calendar" entry (file+headline "/lgg@ihomeonline.net:/home/lgg/gtd/task.org" "Calendar") "* TODO %?\n %i\n %T\n%a")
;        ("d" "Idea" entry (file+headline "/lgg@ihomeonline.net:/home/lgg/gtd/task.org" "Idea") "* TODO %?\n %i\n %T\n%a")
;        ("f" "Finished" entry (file "/lgg@ihomeonline.net:/home/lgg/gtd/finished.org") "* TODO %?\n %i\n %T\n")
;        ("i" "Prj-idev" entry (file+headline "/lgg@ihomeonline.net:/home/lgg/gtd/project.org" "Idev") "* TODO %?\n %i\n %T\n")
;        ("h" "Prj-ihomed" entry (file+headline "/lgg@ihomeonline.net:/home/lgg/gtd/project.org" "Ihomed") "* TODO %?\n %i\n %T\n")
;        ("s" "Prj-insys" entry (file+headline "/lgg@ihomeonline.net:/home/lgg/gtd/project.org" "Insys") "* TODO %?\n %i\n %T\n")
;        ("p" "Project" entry (file "/lgg@ihomeonline.net:/home/lgg/gtd/project.org") "* TODO %?\n %i\n %T\n")))

;打开org-capture
;(global-set-key "\C-cc" 'org-capture)

;; 设置org-agenda  Trash.org保存被丢弃的任务
(setq org-agenda-files
      '( "~/gtd/inbox.org"
         "~/gtd/task.org"
         "~/gtd/finished.org"
         "~/gtd/project.org"
         "~/gtd/trash.org"))
; 打开日程表
;(global-set-key "\C-ca" 'org-agenda)
;;将org日程加入appt提醒
;;(org-agenda-to-appt)
;---------------------- GTD配置结束 --------------------------------------------

;; 拷贝创建gtd文件的本地副本
(defun load-gtd-files (hostname)
  "Copy gtd files to local"
  (interactive "s hostname: ")
  ;默认使用ihomeonline.net作为服务器
  (if (string= hostname "")
      (setq hostname "ihomeonline.net"))
  (setq hostpath (concat "/lgg@" hostname ":/home/lgg/gtd/"))
  (copy-file (concat hostpath "inbox.org") "~/gtd/inbox.org" t)
  (copy-file (concat hostpath "task.org") "~/gtd/task.org" t)
  (copy-file (concat hostpath "finished.org") "~/gtd/finished.org" t)
  (copy-file (concat hostpath "project.org") "~/gtd/project.org" t)
  (copy-file (concat hostpath "trash.org") "~/gtd/trash.org" t))
;; 本地GTD修改上传到云端
(defun put-gtd-files (hostname)
  "Copy gtd files to server"
  (interactive "s hostname: ")
  ;默认使用ihomeonline.net作为服务器
  (if (string= hostname "")
      (setq hostname "ihomeonline.net"))
  (setq hostpath (concat "/lgg@" hostname ":/home/lgg/gtd/"))
  (copy-file "~/gtd/inbox.org" (concat hostpath "inbox.org") t)
  (copy-file "~/gtd/task.org" (concat hostpath "task.org") t)
  (copy-file "~/gtd/finished.org" (concat hostpath "finished.org") t)
  (copy-file "~/gtd/project.org" (concat hostpath "project.org") t)
  (copy-file "~/gtd/trash.org" (concat hostpath "trash.org") t))

;; 配置mobileorg
;; 获取云端文件 org-mobile-pull
;; 文件上传到云端 org-mobile-push
(setq org-mobile-directory "/lgg@ihomeonline.net:/home/backups/GTDsync/")
(setq org-mobile-index-file "index.org")
(setq org-mobile-inbox-for-pull "/lgg@ihomeonline.net:/home/backups/GTDsync/fromMobile.org")
;; 注意org-mobile-files必须是在org-directory目录下
;; 保存到gtd-sync文件夹中的文件会包括去掉org-directory以后的路径
;; 所以org-directory必须为org-mobile-files的直接路径，否则手机端读取会出错
;; org-directory必须是本地路径，否则checknum.dat文件会出错(应该是没有权限打开文件所以不能创建checknum.dat中的记录)，不能通过。
;; 鉴于系统的不完善，调用org-mobile-pull和org-moblie-push必须在云端计算机执行。
;; 本地先调用load-gtd-files()再调用org-agenda生成日程表
(setq org-directory "~/gtd/")
(setq org-mobile-files '("~/gtd/inbox.org"
                         "~/gtd/task.org"
                         "~/gtd/project.org"
                         "~/gtd/finished.org"
                         "~/gtd/trash.org"))

;; 简单的做一些记录和笔记
;;(setq remember-data-file "/lgg@ihomeonline.net:/home/backups/notes.org")
(setq remember-data-file "~/gtd/note")

;----------------------- org快捷键开始 --------------------------------------------
; C-c C-s创建一个任务计划时间
; C-c C-d输入截止时间
; C-u C-c C-c 将一个列表项变为复选框  C-c C-c改变复选框状态
;----------------------- org快捷键结束 --------------------------------------------
;; ----------------------设置一些org-mode使用的快捷键------------------------------
;; 打开org-capture
(global-set-key "\C-cc" 'org-capture)
;; 创建链接
(global-set-key "\C-cl" 'org-store-link)
;; 打开日程表
(global-set-key "\C-ca" 'org-agenda)
;; 定义一些org-mode的快捷键
(defun org-mode-set-key ()
  ;; 记录一个地址，以便快速跳转
  (local-set-key "\C-crp" 'org-mark-ring-push)
  ;; 跳转到一个记录的地址
  (local-set-key "\C-crg" 'org-mark-ring-goto))
;; 将快捷键绑定到org-mode中
(add-hook 'org-mode-hook 'org-mode-set-key)
;; ----------------------设置一些org-mode使用的快捷键 结束--------------------------

(provide 'init-org)
